package com.school.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.school.entity.HljtDevice;
import com.school.entity.HljtVersion;
import com.school.entity.web.HljtDeviceManagement;
import com.school.dao.HljtDeviceDao;
import com.school.dao.HljtOtherInterfacesDao;
import com.school.service.HljtDeviceService;
import com.school.util.WhetherNotToUpdate;

@Service
public class HljtDeviceServiceImpl implements HljtDeviceService{
	
	@Autowired
	private HljtDeviceDao hljtDeviceDao;
	@Autowired
	private HljtOtherInterfacesDao hljtOtherInterfacesDao;
	@Override
	@Transactional(readOnly=true)
	public HljtDevice equipmentRegistration(HljtDevice hljtDevice) {
		HljtDevice hljtDeviceE;
		if(null==hljtDevice.getMac()) {
			hljtDeviceE = null;
		}else {
			hljtDeviceE =  hljtDeviceDao.equipmentRegistration(hljtDevice);
		}
			return	hljtDeviceE;
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ)
	public int addDevice(HljtDevice hljtDevice) {
		int temp ;
		if(null==hljtDevice) {
			temp = -1;
		}else {
			temp =  hljtDeviceDao.addDevice(hljtDevice);
		}
			return	temp;
	}
	@Override
	public Map<String, Object> findDeviceEX(String versionCode,String versionname ,String mac) throws Exception {
			Map<String, Object>map = new HashMap<>();
			//1是更新 0是不更新 
			HljtDevice device = hljtDeviceDao.findDevice(mac);
			if(null!=device&&null!=device.getVersioncode()) {
				HljtVersion latestVersion = hljtOtherInterfacesDao.latestVersion();
				if(	Integer.parseInt(latestVersion.getVersionCode())>Integer.parseInt(versionCode)) {
					map.put("isUpdate", 1);
					map.put("updateUrl", WhetherNotToUpdate.getIp()+latestVersion.getPacketAddress());
					map.put("msg", "版本地址");
				}else {
					HljtDevice hljtDevice = new HljtDevice();
					hljtDevice.setVersioncode(versionCode);
					hljtDevice.setUpdate(1);
					hljtDevice.setVersionname(versionname);
					hljtDevice.setMac(mac);
					hljtDeviceDao.updateDevice(hljtDevice);
					map.put("isUpdate", 0);
					map.put("updateUrl",null);
					map.put("msg", "更新成功");
				}
			}else {
				map.put("isUpdate", 0);
				map.put("updateUrl",null);
				map.put("msg", "当前设备版本为空");
			}
		return map;
	}
	
	@Override
	public HljtDevice findDevice(String Mac) {
		if(null !=hljtDeviceDao.findDevice(Mac)) {
			return hljtDeviceDao.findDevice(Mac);
		}
		return null;
	}
	

	@Override
	@Transactional
	public int updateDevice(HljtDevice hljtDevice) {
		return hljtDeviceDao.updateDevice(hljtDevice);
	}

	@Override
	public Integer updateDeviceUrl(HljtDevice hljtDevice) {
		if(hljtDeviceDao.updateDeviceUrl(hljtDevice)>0) {
			return hljtDeviceDao.updateDeviceUrl(hljtDevice);
		}
		return null;
	}

	@Override
	public Map<String , Object> deviceNotBoundClass() {
		Map<String , Object> map = new HashMap<>();
		map.put("deviceInfo", hljtDeviceDao.deviceNotBoundClass());
		map.put("classInfo", hljtDeviceDao.classesWithoutBindingDevices());
		hljtDeviceDao.classesWithoutBindingDevices();
		return map;
	}

	@Override
	public Map<String , Object> deviceBindClassInfo(HljtDeviceManagement hljtDeviceManagement) {
		Map<String , Object> map = new HashMap<>();
		List<HljtDeviceManagement> temp = hljtDeviceDao.deviceBindClassInfo(hljtDeviceManagement);
		for (HljtDeviceManagement dev : temp) {
			dev.setModulesList(hljtDeviceDao.devicetaskByMac(dev));
		}
		map.put("deviceInfo",temp);
		map.put("classInfo", hljtDeviceDao.classesWithoutBindingDevices());
		map.put("count", hljtDeviceDao.deviceBindClassCount());
		
		return map;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean addDevicetask(List<HljtDeviceManagement> list){
		List<HljtDeviceManagement> temp = new ArrayList<>();
		for (HljtDeviceManagement hljtDeviceManagement : list) {
			if(null != hljtDeviceManagement.getModulesId()) {
				temp.add(hljtDeviceManagement);
			}
			
		}
		hljtDeviceDao.deletetDevicetaskByMacAndClassId(list);
		Integer d = 0;
		if( null !=temp && temp.size()>0) {
			d = hljtDeviceDao.addDevicetask(temp);
		}
		if(d == temp.size() ) {
			return true;
		}
		return false;
	}

	@Override
	public Integer updateDeviceByMac(List<HljtDeviceManagement> hljtDeviceManagement) {
		if(null != hljtDeviceManagement && hljtDeviceManagement.size()>0) {
			return	hljtDeviceDao .updateDeviceByMac(hljtDeviceManagement);
		}
		return 0;
	}

	@Override
	public Integer updateDeviceByMavAndClassId(HljtDevice hljtDevice) {
		return hljtDeviceDao.updateDeviceByMavAndClassId(hljtDevice);
	}

	@Override
	public HljtDevice allDeviceMacManualStarupTime(String mac) {
		return hljtDeviceDao.allDeviceMacManualStarupTime(mac);
	}

	@Override
	public Integer updateDeviceStateByEndTime() {
	 //return hljtDeviceDao.updateDeviceStateByEndTime(hljtDevice);
		List<HljtDevice> temp = hljtDeviceDao.findDeviceAll();
		List<HljtDeviceManagement>updatelist = new ArrayList<>();
		for (HljtDevice te : temp) {
			if(null!=te.getState()&&te.getState()==2&&new Date().getTime()-te.getEndtime().getTime()>60000) {
				HljtDeviceManagement f = new HljtDeviceManagement();
				f.setMac(te.getMac());
				f.setState(1);
				updatelist.add(f);
			}
		}
		if(null != updatelist && updatelist.size()>0) {
			return hljtDeviceDao.updateDeviceByMac(updatelist);
		}
		return 0;
	}
	

}
